package com.haierCamera.customapplication.ui.main;

import android.content.Context;
import android.content.Intent;
import android.view.LayoutInflater;
import android.view.ViewGroup;

import com.haierCamera.customapplication.R;
import com.haierCamera.customapplication.api.vo.GetDeviceEntity;
import com.haierCamera.customapplication.databinding.HzklAdapterDeviceListItemBinding;
import com.haierCamera.customapplication.ui.custom.BaseListenerOnClik;
import com.haierCamera.customapplication.ui.custom.DataBoundViewHolder;
import com.haierCamera.customapplication.ui.mediaplay.MediaPlayActivity;
import com.haierCamera.customapplication.ui.search.cn.CNPinyinIndex;

import java.util.List;

import androidx.annotation.NonNull;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.FragmentActivity;
import androidx.recyclerview.widget.RecyclerView;

public class HZKLDeviceList2Adapter extends RecyclerView.Adapter<DataBoundViewHolder<HzklAdapterDeviceListItemBinding>> {

//    private List<GetDeviceEntity> data;
    private List<CNPinyinIndex<GetDeviceEntity>> data;
    private Context context;
    private FragmentActivity activity;
    private BaseListenerOnClik click;
    private boolean isEdit;

    public HZKLDeviceList2Adapter(Context context) {
        this.context = context;
    }

    public void setActivity(FragmentActivity activity) {
        this.activity = activity;
    }

//    public void upDate(List<GetDeviceEntity> data) {
//        this.data = data;
//        notifyDataSetChanged();
//    }

    public void upDate(List<CNPinyinIndex<GetDeviceEntity>> data) {
        this.data = data;
        notifyDataSetChanged();
    }

    public List<CNPinyinIndex<GetDeviceEntity>> getData() {
        return data;
    }

    public void setOnItemOnClick(BaseListenerOnClik click) {
        this.click = click;
    }

    @Override
    public DataBoundViewHolder<HzklAdapterDeviceListItemBinding> onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        HzklAdapterDeviceListItemBinding binding = DataBindingUtil.inflate(LayoutInflater.from(context)
                , R.layout.hzkl_adapter_device_list_item, viewGroup, false);
        return new DataBoundViewHolder<>(binding);
    }

    @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public void onBindViewHolder(@NonNull DataBoundViewHolder<HzklAdapterDeviceListItemBinding> holder, int i) {
//        holder.binding.tvName.setText(data.get(i).getName());
        holder.binding.tvName.setText(data.get(i).cnPinyin.data.name +"");
        holder.binding.tvSn.setText(data.get(i).cnPinyin.data.serialNumber + "");
//        holder.binding.tvName.setText(data.get(i).name +"");
//        holder.binding.tvSn.setText(data.get(i).serialNumber + "");
        holder.itemView.setOnClickListener((v) -> {
            click.onClick(i);
//            context.startActivity(new Intent(context, MediaPlayActivity.class)
//                    .putExtra("TYPE",1)
//                    .putExtra("RESID", "0")
//                    .putExtra("RESDEVICEID", data.get(i).cnPinyin.data.serialNumber)
//                    .putExtra("RESPWD", data.get(i).cnPinyin.data.securityCode));
        });

    }

    @Override
    public int getItemCount() {
        if (data != null) {
            return data.size();
        } else {
            return 0;
        }
    }
}